package net.mobgame.android.loadfile;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import net.mobgame.common.Utils;
import android.os.AsyncTask;

public class DownloadFromMobgame extends AsyncTask<String, String, String> {

	@Override
	protected String doInBackground(String... arrayUrl) {
		int count;
		OutputStream output = null;
		try {
			URL url = new URL(arrayUrl[0]);
			URLConnection connection = url.openConnection();
			connection.connect();
			int lengthOfFile = connection.getContentLength();
			InputStream input = new BufferedInputStream(url.openStream());
			if (Utils.hasStorage()) {
				String path = Utils.makeTempFolder();
				output = new FileOutputStream(path);
			}
			byte[] data = new byte[1024];
			long total = 0;
			while ((count = input.read(data)) != -1) {
				total += count;
				publishProgress(""+(int)((total*100)/lengthOfFile));
				output.write(data, 0, count);
			}
			output.flush();
			output.close();
			input.close();
		} catch (Exception e) {	}
		return null;
	}

}
